#!/usr/bin/perl
use strict;
use modules::encode;
package csvoice;

sub csvoice {

    my ($dbh,$remote,$input,$csnum) = (shift,shift,shift,shift);
    my $sth;my $auth;
    if ($$input[4] eq "") {
	print $remote "$csnum O $$input[0] :Syntax :VOICE <channel> <nick>\n";
    }
    else {
	$sth=$dbh->prepare("SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
	$sth->execute;
	if (!($auth = $sth->fetchrow_array)) {
	    print $remote "$csnum O $$input[0] :You have not identified yourself with nickserv.\n";
	}
	else { 
	    $sth=$dbh->prepare("SELECT level FROM users WHERE channel = '$$input[4]' AND rnick = '$auth' AND level >= 100");
	    $sth->execute;
	    if (!$sth->fetchrow_array) {
		print $remote "$csnum O $$input[0] :Access denied.\n";
	    }
	    else {
		$sth=$dbh->prepare("SELECT num FROM ninfo WHERE nick = '$$input[5]';");
		$sth->execute;
		my $tmpnum = $sth->fetchrow_array;
		$sth=$dbh->prepare("SELECT num FROM onchan WHERE num = '$tmpnum' AND code = '".encode::encode($$input[0])."' AND isvoice = '0' AND channel = '$$input[4]';");
		$sth->execute;
		if (!$sth->fetchrow_array) {
		    print $remote "$csnum O $$input[0] :$$input[5] is not devoiced in $$input[4]/\n";
		}
		else {
		    print $remote "$csnum M $$input[4] +v $tmpnum\n";
		    print $remote "$csnum O $$input[0] :Gave $$input[5] voice on $$input[4].\n";
		    $sth=$dbh->prepare("UPDATE onchan SET isvoice = '1' WHERE num = '$tmpnum' AND channel = '$$input[4]' AND code = '".encode::encode($tmpnum)."';");
		    $sth->execute;
		}
	    }
	}
    }

}
1;
